<?php

namespace app\supplier\controller;

use app\erp\model\MonthContractdb;
use app\erp\model\Partnerdb;
use app\erp\model\Sitedb;
use app\erp\model\Goodsdb;
use app\erp\model\Categorydb;
use app\erp\model\Orderdb;
use app\erp\model\Memberdb;
use think\Cache;
use think\Config;
use think\Db;
use app\erp\model\Articledb;

class Index extends Base
{

    public $post = array();

    public function _initialize()
    {
        parent::_initialize();

        $request = request();
        $param = $request->param();
        $this->post = $param;
    }

    public function clearCache()
    {
        Cache::clear();
        $this->layuiReturn('清除缓存成功', 1, 'success', 1);
    }

    public function index()
    {
        if (PARTNER_ID) {
            $partner = new Partnerdb();
            $get_partner = $partner->ajaxFind(PARTNER_ID);
            $this->assign('get_partner', $get_partner);
        }
        // 获取分站信息
        $sitedb = new Sitedb();
        $get_site = $sitedb->ajaxFind(SITE_ID);
        $this->assign('get_site', $get_site);

        $pianyuan = Db::name('site_config')->where('config_name', 'PIANYUAN')->value('config_value');
        $this->assign('pianyuan', str_replace("\n", ',', $pianyuan));
        $this->assign('supplier_id', MEMBER_ID);

        // 当前id是否可搜索业务员
        $kk_admin_id = Db::name('site_config')->where('config_name', 'KK_ADMIN_ID')->value('config_value');
        $this->assign('is_kk', in_array(MEMBER_ID, explode(',', $kk_admin_id)) ? 'yes' : 'no');

        return $this->fetch();
    }

    // 搜索业务员
    public function yewuyuan()
    {
        $memberdb = new Memberdb();
        $get_member = $memberdb->whereLike('supplier_name', '%' . $this->post['supplier_name'] . '%')->limit(0, 5)->column('supplier_name');
        $this->success($get_member);
    }

    public function welcome()
    {
        $articledb = new Articledb ();
        $articledb->param ['artclass_id'] = 11;
        $articledb->param ['page'] = 1;
        $articledb->param ['limit'] = 10;
        $articledb->param ['_o'] = 'article_id';
        $articledb->param ['_b'] = 'desc';
        $get_art = $articledb->ajaxList();
        $this->assign('get_art', $get_art);
        $service_config['server_software'] = request()->server()['SERVER_SOFTWARE'];
        $service_config['php_ver'] = PHP_VERSION;
        $service_config['upload_size'] = get_cfg_var("upload_max_filesize") ? get_cfg_var("upload_max_filesize") : "不允许上传附件";
        $service_config['memory_limit'] = get_cfg_var("memory_limit") ? get_cfg_var("memory_limit") : "无";
        $service_config['server_time'] = date("Y-m-d H:i:s");
        $db = new Db();
        $sql_v = $db::query('select VERSION() as ver');
        $service_config['sql_ver'] = $sql_v[0]['ver'];
        $this->assign('service_config', $service_config);
        return $this->fetch();
    }

    public function artdetail($article_id)
    {
        $articledb = new Articledb();
        $articledb->param['article_id'] = $article_id;
        $get_art = $articledb->ajaxFind();
        $this->assign('get_art', $get_art);
        return $this->fetch();
    }

    public function count()
    {
        return $this->fetch();
    }

    // 统计分站产品数据
    public function countSiteGoods()
    {
        // 获取分站
        $sitedb = new Sitedb();
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 100;
        $data = array();
        $all_num = 0;
        $goodsdb = new Goodsdb();
        if (SITE_ID != null) {
            $sitedb->param['site_id'] = SITE_ID;
        } else {
            $goodsdb->param['site_id'] = '';
            $temp_count = $goodsdb->ajaxCount();
            $temp['name'] = '主站';
            $temp['value'] = $temp_count;
            $temp['site_id'] = '';
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $get_site_list = $sitedb->ajaxSelect();

        foreach ($get_site_list['data'] as $key => $val) {
            $goodsdb->param['site_id'] = $val['site_id'];
            $temp_count = $goodsdb->ajaxCount();
            $temp['name'] = $val['site_name'];
            $temp['value'] = $temp_count;
            $temp['site_id'] = $val['site_id'];
            $temp['type'] = 'site';
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $this->layuiReturn($data, $all_num, 'success', 1);
    }

    // 统计分类产品数据
    public function countCategoryGoods()
    {
        $site_id = isset($this->post['site_id']) ? $this->post['site_id'] : null;
        $parent_sign = isset($this->post['parent_sign']) ? $this->post['parent_sign'] : 0;
        $categorydb = new Categorydb();
        $categorydb->param['site_id'] = $site_id;
        $categorydb->param['parent_sign'] = $parent_sign;
        $get_category_info = $categorydb->ajaxList();
        $data = array();
        $all_num = 0;
        $goodsdb = new Goodsdb();
        $goodsdb->param['site_id'] = $site_id;
        foreach ($get_category_info as $key => $val) {
            $goodsdb->param = array();
            if ($val['level'] == 1) {
                $goodsdb->param['category_sign1'] = $val['category_sign'];
            } elseif ($val['level'] == 2) {
                $goodsdb->param['category_sign2'] = $val['category_sign'];
            } else {
                $goodsdb->param['category_sign3'] = $val['category_sign'];
            }
            $temp_count = $goodsdb->ajaxCount();
            $temp['name'] = $val['category_name'];
            $temp['value'] = $temp_count;
            $temp['parent_sign'] = $val['category_sign'];
            $temp['type'] = 'category';
            $temp['site_id'] = $site_id;
            $temp['level'] = $val['level'];
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $this->layuiReturn($data, $all_num, 'success', 1);
    }

    // 统计分站订单
    public function countSiteOrder()
    {
        $type = $this->post['type'];
        // 获取分站
        $sitedb = new Sitedb();
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 100;
        $data = array();
        $all_num = 0;
        $orderdb = new Orderdb();
        if (SITE_ID != null) {
            $sitedb->param['site_id'] = SITE_ID;
        } else {
            $orderdb->param['site_id'] = '';
            if ($type == 'price')
                $temp_count = $orderdb->ajaxPriceCount() / 100;
            else
                $temp_count = $orderdb->ajaxCount();
            $temp['name'] = '主站';
            $temp['value'] = $temp_count;
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $get_site_list = $sitedb->ajaxSelect();

        foreach ($get_site_list['data'] as $key => $val) {
            $orderdb->param['site_id'] = $val['site_id'];
            if ($type == 'price')
                $temp_count = $orderdb->ajaxPriceCount() / 100;
            else
                $temp_count = $orderdb->ajaxCount();
            $temp['name'] = $val['site_name'];
            $temp['value'] = $temp_count;
            array_push($data, $temp);
            $all_num += $temp_count;
        }
        $this->layuiReturn($data, $all_num, 'success', 1);
    }

    // 统计会员
    public function countMember()
    {
        $memberdb = new Memberdb();
        $memberdb->param['site_id'] = SITE_ID;
        $count = $memberdb->ajaxCount();
        $this->layuiReturn($count, $count, 'success', 1);
    }

    // 统计销量前20
    public function countGoodsTop20()
    {
        // 获取分站
        $sitedb = new Sitedb();
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 100;
        $data = array();
        $y_data = array();
        $goodsdb = new Goodsdb();
        $goodsdb->param['_o'] = 'sale_num';
        $goodsdb->param['_b'] = 'desc';
        $goodsdb->param['page'] = 1;
        $goodsdb->param['limit'] = 20;
        if (SITE_ID != null) {
            $sitedb->param['site_id'] = SITE_ID;
        } else {
            $goodsdb->param['site_id'] = '';
            $data_goods = $goodsdb->ajaxList();
            $temp['name'] = '主站';
            $temp['type'] = 'bar';
            $temp['data'] = array();
            foreach ($data_goods as $key => $val) {
                $t1['name'] = $val['goods_name'];
                $t1['value'] = $val['sale_num'];
                array_push($temp['data'], $t1);
            }
            array_push($data, $temp);
        }
        $get_site_list = $sitedb->ajaxSelect();
        for ($i = 1; $i <= 20; $i++) {
            array_push($y_data, 'Top' . $i);
        }
        foreach ($get_site_list['data'] as $k => $v) {
            $goodsdb->param['site_id'] = $v['site_id'];
            $data_goods = $goodsdb->ajaxList();
            $temp['name'] = $v['site_name'];
            $temp['type'] = 'bar';
            $temp['data'] = array();
            foreach ($data_goods as $key => $val) {
                $t1['name'] = $val['goods_name'];
                $t1['value'] = $val['sale_num'];
                array_push($temp['data'], $t1);
                // array_push($y_data, $val['goods_sku']);
            }
            array_push($data, $temp);
        }
        $this->layuiReturn($data, 0, $y_data, 1);
    }

    public function countSiteOrderList()
    {
        // 获取分站
        $sitedb = new Sitedb();
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 100;
        $data = array();
        $all_num = 0;
        $all_price = 0;
        $xAxis = array();
        $legend = array();
        $new_site_arr = array();
        // 30天
        if ($this->post['select_types'] == 'day') {
            $start_date = strtotime('-30 day');
            for ($i = 30; $i >= 0; $i--) {
                array_push($xAxis, date('m月d', strtotime('-' . $i . ' day')));
            }
        } else {
            $start_date = strtotime('-12 month');
            for ($i = 12; $i >= 0; $i--) {
                array_push($xAxis, date('y年m', strtotime('-' . $i . ' month')));
            }
        }
        if (SITE_ID != null) {
            $sitedb->param['site_id'] = SITE_ID;
        } else {
            array_push($legend, '主站数量');
            array_push($legend, '主站金额');
            array_push($new_site_arr, array(
                'site_id' => '',
                'site_name' => '主站'
            ));
        }
        $get_site_list = $sitedb->ajaxSelect();
        foreach ($get_site_list['data'] as $key => $val) {
            array_push($legend, $val['site_name'] . '数量');
            array_push($legend, $val['site_name'] . '金额');
            array_push($new_site_arr, array(
                'site_id' => $val['site_id'],
                'site_name' => $val['site_name']
            ));
        }
        $orderdb = new Orderdb();
        foreach ($new_site_arr as $key => $val) {
            $temp_price['name'] = $val['site_name'] . '金额';
            $temp_price['type'] = 'line';
            $temp_price['yAxisIndex'] = 1;
            $temp_price['data'] = array();
            $temp_num['name'] = $val['site_name'] . '数量';
            $temp_num['type'] = 'bar';
            $temp_num['data'] = array();
            $orderdb->param['site_id'] = $val['site_id'];
            if ($this->post['select_types'] == 'day') {
                for ($i = 30; $i >= 0; $i--) {
                    $temp_date = strtotime('-' . $i . ' day');
                    $orderdb->param['create_time_start'] = date('Y-m-d', $temp_date);
                    $orderdb->param['create_time_end'] = date('Y-m-d', $temp_date);
                    $get_price_count = $orderdb->ajaxPriceCount() / 100;
                    array_push($temp_price['data'], $get_price_count);
                    $get_num_count = $orderdb->ajaxCount();
                    array_push($temp_num['data'], $get_num_count);
                }
            } else {
                for ($i = 12; $i >= 0; $i--) {
                    $temp_date = strtotime('-' . $i . ' month');
                    $orderdb->param['create_time_start'] = date('Y-m', $temp_date) . '-01';
                    $orderdb->param['create_time_end'] = date('Y-m', $temp_date) . '-31';
                    $get_price_count = $orderdb->ajaxPriceCount() / 100;
                    array_push($temp_price['data'], $get_price_count);
                    $get_num_count = $orderdb->ajaxCount();
                    array_push($temp_num['data'], $get_num_count);
                }
            }
            array_push($data, $temp_price);
            array_push($data, $temp_num);
        }
        $this->layuiReturn($data, $legend, $xAxis, 1);
    }

    /**
     * Title("校验是否有月度合同没有读取")
     */
    public function checkMonthContract()
    {
        $monthdb = new MonthContractdb();
        $chk = $monthdb->where('supplier_id', MEMBER_ID)
            ->where('is_read', 2)
            ->count();
        if ($chk > 0) {
            $get_contract = $monthdb->where('supplier_id', MEMBER_ID)
                ->where('is_read', 2)
                ->find();
            $get_contract->save(['is_read' => 1]);
        }
        $this->layuiReturn($chk);
    }
}